<template>
<!--  <h2>检查组管理</h2>-->
  <div>
    <div class="content-header">
      <h1>预约管理<small>检查组管理</small></h1>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>预约管理</el-breadcrumb-item>
        <el-breadcrumb-item>检查组管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="app-container">
      <div class="box">
        <div class="filter-container">
          <el-input placeholder="编码/名称/助记码" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
          <el-button @click="findPage()" class="dalfBut">查询</el-button>
          <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
        </div>
        <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
          <el-table-column type="index" align="center" label="序号"></el-table-column>
          <el-table-column prop="code" label="检查组编码" align="center"></el-table-column>
          <el-table-column prop="name" label="检查组名称" align="center"></el-table-column>
          <el-table-column label="适用性别" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="helpCode" label="助记码" align="center"></el-table-column>
          <el-table-column prop="remark" label="说明" align="center"></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination
              class="pagiantion"
              @current-change="handleCurrentChange"
              :current-page="pagination.currentPage"
              :page-size="pagination.pageSize"
              layout="total, prev, pager, next, jumper"
              :total="pagination.total">
          </el-pagination>
        </div>
        <!-- 新增标签弹层 -->
        <div class="add-form">
          <el-dialog title="新增检查组" :visible.sync="dialogFormVisible">
            <template>
              <el-tabs v-model="activeName" type="card">
                <el-tab-pane label="基本信息" name="first" to="/appointment/group_management/group_info">
                  <group-info></group-info>
                </el-tab-pane>
                <el-tab-pane label="检查项信息" name="second" to="/appointment/group_management/project_info">
                  <project-info></project-info>
                </el-tab-pane>
              </el-tabs>
            </template>
            <div slot="footer" class="dialog-footer">
              <el-button @click="close">取消</el-button>
              <el-button type="primary" @click="handleAdd()">确定</el-button>
            </div>
          </el-dialog>
        </div>

        <!-- 编辑标签弹层 -->
        <div class="add-form">
          <el-dialog title="编辑检查组" :visible.sync="dialogFormVisible4Edit">
            <template>
              <el-tabs v-model="activeName" type="card">
                <el-tab-pane label="基本信息" name="first">
                  <group-info></group-info>
                </el-tab-pane>
                <el-tab-pane label="检查项信息" name="second">
                  <project-info></project-info>
                </el-tab-pane>
              </el-tabs>
            </template>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
              <el-button type="primary" @click="handleEdit()">确定</el-button>
            </div>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import groupInfo from "@/components/GroupInfo";
import projectInfo from "@/components/ProjectInfo";
import GroupInfo from "@/components/GroupInfo";
import ProjectInfo from "@/components/ProjectInfo";
export default {
  name: "GroupManagement",
  components: {ProjectInfo, GroupInfo},
  data(){
    return {
      activeName:'first',//添加/编辑窗口Tab标签名称
      pagination: {//分页相关属性
        currentPage: 1,
        pageSize:10,
        total:0,
        queryString:null,
      },
      dataList: [],//列表数据
      formData: {},//表单数据
      tableData:[],//新增和编辑表单中对应的检查项列表数据
      checkitemIds:[],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
      dialogFormVisible: false,//控制添加窗口显示/隐藏
      dialogFormVisible4Edit:false,//控制编辑窗口显示/隐藏,
      headers:this.$cookies.get("token") != null ? this.$cookies.get("token") : '',
    }
  },
  created() {
    this.findPage();
  },
  mounted() {
    /*this.$bus.$on("formData",(data)=>{
      console.log(data)
    })*/
  },
  beforeDestroy() {
    // this.$bus.$off()
  },

  methods: {
    close(){
      this.dialogFormVisible = false
      this.$bus.$emit("clear","清除表单")

    },
    //编辑
    handleEdit() {
      this.$bus.$emit("clear","清除表单")
      this.$axios.post(`/api/checkgroup/edit.do?ids=${this.$store.state.checkitemIds}`,this.$store.state.formData,{
        headers:{
          'token':this.headers
        }
      }).then((res)=>{
        if(res.data.flag){
          this.dialogFormVisible = false
          this.$message({
            message:res.data.message,
            type:'success'
          })
          this.$bus.$emit("clear","清除表单")
        }else{
          this.$message.error(res.data.message)
        }
      }).finally(()=>{
        this.findPage()
      })
    },
    //添加
    handleAdd () {
      this.$bus.$emit("clear","清除表单")
      this.activeName = 'first'
      // this.$bus.$emit("post","快给我传递数据")
      this.$axios.post(`/api/checkgroup/add.do?ids=${this.$store.state.checkitemIds}`,this.$store.state.formData,{
        headers:{
          'token':this.headers
        }
      }).then((res)=>{
        if(res.data.flag){
          this.dialogFormVisible = false
          this.$message({
            message:res.data.message,
            type:'success'
          })
          this.$bus.$emit("clear","清除表单")
        }else{
          this.$message.error(res.data.message)
        }
      }).finally(()=>{
        this.findPage()
      })
    },
    //分页查询
    findPage() {
      var param = {
        currentPage: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
        queryString: this.pagination.queryString
      }
      this.$axios.post('/api/checkgroup/findPage.do',param,{
        headers:{
          'token':this.headers
        }
      }).then((res)=>{
        this.dataList = res.data.rows
        this.pagination.total = res.data.total
      })
    },
    // 弹出添加窗口
    handleCreate() {
      this.$bus.$emit("clear","清除表单")
      this.dialogFormVisible = true
    },
    // 弹出编辑窗口
    handleUpdate(row) {
      this.dialogFormVisible4Edit = true
      this.$bus.$emit("edit_id",row.id)
    },
    //切换页码
    handleCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage
      this.findPage()
    },
    // 删除
    handleDelete(row) {
      this.$confirm("确定删除吗?","提示",{
        type:'warning',
      }).then(()=>{
        this.$axios.delete(`/api/checkgroup/delOne.do?id=${row.id}`,{
          headers:{
            'token':this.headers
          }
        }).then((res)=>{
          if(res.data.flag){
            this.dataList = this.dataList.filter((item)=>{
              return item.id !== row.id
            })
            this.$message.success({
              message:res.data.message,
              type:'success'
            })
          }else{
            this.$message.error(res.data.message)
          }
        })
      }).catch(()=>{
        this.$message({
          type:'info',
          message:'已取消删除'
        })
      })
    }
  },
}
</script>

<style scoped>

</style>